// 物资领用申请记录
var jreclistColumns = [{
  id: 'proName',
  title: '项目名称',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'userName',
  title: '申请人',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'time',
  title: '申请时间',
  type: 'string',
  width: 110,
  columnClass: 'text-center',
}, {
  id: 'msg',
  title: '申请理由',
  type: 'string',
  columnClass: 'text-left',
}, {
  id: 'operation',
  title: '操作',
  width: 70,
  type: 'string',
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '';
    content += '<a class="btn btn-xs btn-primary" href="#/selrec"><i class="icon-eye-open"></i>  查看</a>';
    return content;
  }
}];
var jreclistOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '物资领用申请记录',
  datas: [{
    proName: '项目A',
    userName: '张三',
    time: '2018-01-02',
    msg: '申请理由内容测试数据'
  }],
  columns: jreclistColumns,
  gridContainer: 'jreclistContainer',
  toolbarContainer: 'jreclistToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var jreclistGrid = $.fn.dlshouwen.grid.init(jreclistOption);
$(function () {
  jreclistGrid.load();
});

// 物资ID记录数组
var tidlist = [];
// 合计金额初始值
var sum = 0;
// 购物车
function getline(id, name, price, number) {
  // 检测清单
  if (tidlist.length == 0) {
    // 加入清单-赋予初始值
    var tnumber = 1
    $("#reclbox").append(`
      <li id="` + id + `">
        <label>` + name + `</label>
        <label>单价：￥<span>` + price + `</span>元</label>
        <label>数量：<span class="tsize">` + tnumber + `</span>/` + number + `</label>
        <label><button class="btn btn-xs btn-danger" onclick="cdel(` + id + `,` + price + `,` + tnumber + `);"><i class="fa fa-trash-o"></i> 删除</button></label>
      </li>
    `);
    // 记录物资ID
    tidlist.push(id)
    var newArr = [];
    for (var i = 0; i < tidlist.length; i++) {
      if (newArr.indexOf(tidlist[i]) == -1) {
        newArr.push(tidlist[i]);
      }
    }
    tidlist = newArr
    // 计算合计金额
    sum = parseFloat(price);
    $('.sumprc').html(sum.toFixed(2));
    return;
  }
  // 检测清单-不存在重复物资
  if (tidlist.indexOf(id) === -1) {
    // 加入清单-赋予初始值
    var tnumber = 1
    $("#reclbox").append(`
      <li id="` + id + `">
        <label>` + name + `</label>
        <label>单价：￥<span>` + price + `</span>元</label>
        <label>数量：<span class="tsize">` + tnumber + `</span>/` + number + `</label>
        <label><button class="btn btn-xs btn-danger" onclick="cdel(` + id + `,` + price + `,` + tnumber + `);"><i class="fa fa-trash-o"></i> 删除</button></label>
      </li>
    `);
    // 记录物资ID
    tidlist.push(id)
    var newArr = [];
    for (var i = 0; i < tidlist.length; i++) {
      if (newArr.indexOf(tidlist[i]) == -1) {
        newArr.push(tidlist[i]);
      }
    }
    tidlist = newArr
    // 计算合计金额
    sum = parseFloat(sum) + parseFloat(price);
    $('.sumprc').html(sum.toFixed(2));
    return;
  } else {
    // 累加清单物资数量
    $('#reclbox').find('li').each(function () {
      var tid = $(this).attr('id');
      if (tid === id) {
        var lnumber = $('#' + tid + ' span.tsize').html();
        // 检测是否超过库存上限
        if (lnumber === number) {
          layer.msg('领取数量不能超过库存量');
        } else {
          // 累加物资数量
          lnumber++;
          $('#' + tid + ' span.tsize').html(lnumber);
          // 计算合计金额
          sum = parseFloat(sum) + parseFloat(price);
          $('.sumprc').html(sum.toFixed(2));

        }
      }
    })
  }
}
// 物资清单-删除操作
function cdel(id, price, tnumber) {
  // 获取选中物资数量
  var lnumber = $('#' + id.id + ' span.tsize').html()
  // 检测数量
  if (lnumber == 1) {
    // 删除清单节点
    $(id).remove();
    // 清楚物资ID记录值
    tidlist.remove(id.id);
    // 计算合计金额
    sum = parseFloat(sum) - parseFloat(price);
    $('.sumprc').html(sum.toFixed(2));
  } else {
    // 减去清单物资数量
    lnumber--
    $('#' + id.id + ' span.tsize').html(lnumber);
    // 计算合计金额
    sum = parseFloat(sum) - parseFloat(price);
    $('.sumprc').html(sum.toFixed(2));
  }
};

// 数组检测方法
Array.prototype.indexOf = function (val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
  }
  return -1;
};
// 数组删除方法
Array.prototype.remove = function (val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

// 领用列表
var letColumns = [{
  id: 'area',
  title: '物资地点',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'materTypeCode',
  title: '物资种类',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'materName',
  title: '物资名称',
  type: 'string',
  // width: 110,
  columnClass: 'text-center',
}, {
  id: 'unitPrice',
  title: '单价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'amount',
  title: '数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'total',
  title: '总价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'operation',
  title: '操作',
  width: 70,
  type: 'string',
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '';
    content += '<a class="btn btn-xs btn-danger" onclick="delItemMater(\''+ record.id+'\')"><i class="icon-trash"></i>  删除</a>';
    return content;
  }
}];



function shLet() {
  var status = $("#letbox").css('display');
  if (status === "none") {
    $("#letbox").show();
  } else {
    $("#letbox").hide();
  }
}
//删除 条目
function delItemMater(id){
    //有没有 更好的 办法
    removeId = id;
    dataInit.exhibitDatas = dataInit.exhibitDatas.filter(removeSameId);
    dataInit.refresh(false);
}

// 审批-领用记录
var letboxColumns = [{
  id: 'address',
  title: '物资地点',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'type',
  title: '物资种类',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'name',
  title: '物资名称',
  type: 'string',
  // width: 110,
  columnClass: 'text-center',
}, {
  id: 'price',
  title: '单价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'number',
  title: '数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'sum',
  title: '总价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}];
var letboxOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '领用列表',
  datas: [{
    address: '南京',
    type: '白酒',
    name: '海之蓝',
    price: '188.00',
    number: '15',
    sum: '2820.00'
  }],
  columns: letboxColumns,
  gridContainer: 'letboxContainer',
  toolbarContainer: 'letboxToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var letboxGrid = $.fn.dlshouwen.grid.init(letboxOption);
$(function () {
  letboxGrid.load();
});

// 领用记录
var letlogColumns = [{
  id: 'address',
  title: '物资地点',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'type',
  title: '物资种类',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'name',
  title: '物资名称',
  type: 'string',
  // width: 110,
  columnClass: 'text-center',
}, {
  id: 'price',
  title: '单价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'number',
  title: '数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'anumber',
  title: '实际领用数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'sum',
  title: '总价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}];
var letlogOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '领用列表',
  datas: [{
    address: '南京',
    type: '白酒',
    name: '海之蓝',
    price: '188.00',
    number: '15',
    anumber: '10',
    sum: '2820.00'
  }],
  columns: letlogColumns,
  gridContainer: 'letlogContainer',
  toolbarContainer: 'letlogToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var letlogGrid = $.fn.dlshouwen.grid.init(letlogOption);
$(function () {
  letlogGrid.load();
});


// 领用记录-确认领用
var lastletColumns = [{
  id: 'address',
  title: '物资地点',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'type',
  title: '物资种类',
  type: 'string',
  width: 75,
  columnClass: 'text-center',
}, {
  id: 'name',
  title: '物资名称',
  type: 'string',
  // width: 110,
  columnClass: 'text-center',
}, {
  id: 'price',
  title: '单价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'number',
  title: '数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'anumber',
  title: '实际领用数量',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
}, {
  id: 'sum',
  title: '总价',
  type: 'string',
  width: 150,
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '￥' + value + '元';
    return content;
  }
}, {
  id: 'operation',
  title: '操作',
  width: 160,
  type: 'string',
  columnClass: 'text-center',
  resolution: function (value, record, column, grid, dataNo, columnNo) {
    var content = '';
    content += '<a class="btn btn-xs btn-info" onclick="shLet()"><i class="icon-edit"></i>  领用确认</a>';
    content += ' ';
    content += '<a class="btn btn-xs btn-danger"><i class="icon-trash"></i>  删除</a>';
    return content;
  }
}];
var lastletOption = {
  lang: 'zh-cn',
  ajaxLoad: false,
  // loadURL:'url',
  exportFileName: '领用列表',
  datas: [{
    address: '南京',
    type: '白酒',
    name: '海之蓝',
    price: '188.00',
    number: '15',
    anumber: '10',
    sum: '2820.00'
  }],
  columns: lastletColumns,
  gridContainer: 'lastletContainer',
  toolbarContainer: 'lastletToolBarContainer',
  tools: '',
  pageSize: 10,
  pageSizeLimit: [10, 15, 20]
};
var lastletGrid = $.fn.dlshouwen.grid.init(lastletOption);
$(function () {
  lastletGrid.load();
});